//
//  UIButton+Extension.swift
//  BTE
//
//  Created by Mike Zhang on 2025/8/14.
//

import UIKit
import UIKit

extension UIButton {
    
    // MARK: - 初始化方法
    
    /// 创建带标题和样式的按钮
    public convenience init(
        title: String,
        font: UIFont = .systemFont(ofSize: 17),
        titleColor: UIColor = .label,
        backgroundColor: UIColor = .clear,
        image: UIImage? = nil,
        contentEdgeInsets: UIEdgeInsets = .zero
    ) {
        self.init(type: .custom)
        configure(
            title: title,
            font: font,
            titleColor: titleColor,
            backgroundColor: backgroundColor,
            image: image,
            contentEdgeInsets: contentEdgeInsets
        )
        sizeToFit()
    }
    
    /// 创建带标题和图标的按钮
    public convenience init(
        title: String,
        image: UIImage?,
        backgroundColor: UIColor = .clear,
        contentEdgeInsets: UIEdgeInsets = .zero
    ) {
        self.init(type: .custom)
        configure(
            title: title,
            font: .systemFont(ofSize: 17),
            titleColor: .label,
            backgroundColor: backgroundColor,
            image: image,
            contentEdgeInsets: contentEdgeInsets
        )
        sizeToFit()
    }
    
    // MARK: - 配置方法
    
    /// 配置按钮所有属性
    public func configure(
        title: String? = nil,
        font: UIFont? = nil,
        titleColor: UIColor? = nil,
        backgroundColor: UIColor? = nil,
        image: UIImage? = nil,
        contentEdgeInsets: UIEdgeInsets? = nil
    ) {
        if let title = title {
            setTitle(title: title)
        }
        if let font = font {
            setFont(font)
        }
        if let titleColor = titleColor {
            setTitleColor(titleColor)
        }
        if let backgroundColor = backgroundColor {
            setBackgroundColor(backgroundColor)
        }
        if let image = image {
            setImage(image: image)
        }
        if let insets = contentEdgeInsets {
            self.contentEdgeInsets = insets
        }
    }
    
    // MARK: - 属性设置方法
    
    /// 设置标题（所有状态）
    public func setTitle(title: String) {
        setTitle(title, for: .normal)
        setTitle(title, for: .highlighted)
        setTitle(title, for: .disabled)
        setTitle(title, for: .selected)
    }
    
    /// 设置标题颜色（所有状态）
    public func setTitleColor(_ color: UIColor) {
        setTitleColor(color, for: .normal)
        setTitleColor(color, for: .highlighted)
        setTitleColor(color, for: .disabled)
        setTitleColor(color, for: .selected)
    }
    
    /// 设置字体
    public func setFont(_ font: UIFont) {
        titleLabel?.font = font
    }
    
    /// 设置图片（所有状态）
    public func setImage(image: UIImage?) {
        setImage(image, for: .normal)
        setImage(image, for: .highlighted)
        setImage(image, for: .disabled)
        setImage(image, for: .selected)
    }
    
    /// 设置图片（通过名称）
    public func setImage(named imageName: String) {
        setImage(image: UIImage(named: imageName))
    }
    
    /// 设置背景颜色（所有状态）
    public func setBackgroundColor(_ color: UIColor) {
        setBackgroundColor(color, for: .normal)
        setBackgroundColor(color, for: .highlighted)
        setBackgroundColor(color, for: .disabled)
        setBackgroundColor(color, for: .selected)
    }
    
    // MARK: - 背景颜色实现
    
    /// 设置背景颜色（指定状态）
    public func setBackgroundColor(_ backgroundColor: UIColor, for state: UIControl.State) {
        // 使用缓存避免重复创建图像
        let colorImage = UIImage.onePixelImage(color: backgroundColor)
        setBackgroundImage(colorImage, for: state)
    }
    
    // MARK: - 其他实用方法
    
    /// 添加圆角和阴影
    public func addCornerAndShadow(
        cornerRadius: CGFloat = 8.0,
        shadowColor: UIColor = .black,
        shadowOpacity: Float = 0.1,
        shadowOffset: CGSize = CGSize(width: 0, height: 2),
        shadowRadius: CGFloat = 4.0
    ) {
        layer.cornerRadius = cornerRadius
        layer.masksToBounds = false
        layer.shadowColor = shadowColor.cgColor
        layer.shadowOpacity = shadowOpacity
        layer.shadowOffset = shadowOffset
        layer.shadowRadius = shadowRadius
    }
    
    /// 添加加载指示器
    public func showLoadingIndicator(style: UIActivityIndicatorView.Style = .medium, color: UIColor? = nil) {
        hideLoadingIndicator()
        
        let indicator = UIActivityIndicatorView(style: style)
        if let color = color {
            indicator.color = color
        }
        indicator.translatesAutoresizingMaskIntoConstraints = false
        indicator.startAnimating()
        indicator.tag = 9999 // 唯一标识符
        
        addSubview(indicator)
        NSLayoutConstraint.activate([
            indicator.centerXAnchor.constraint(equalTo: centerXAnchor),
            indicator.centerYAnchor.constraint(equalTo: centerYAnchor)
        ])
        
        isEnabled = false
        titleLabel?.alpha = 0.0
        imageView?.alpha = 0.0
    }
    
    /// 隐藏加载指示器
    public func hideLoadingIndicator() {
        if let indicator = viewWithTag(9999) as? UIActivityIndicatorView {
            indicator.stopAnimating()
            indicator.removeFromSuperview()
        }
        
        isEnabled = true
        titleLabel?.alpha = 1.0
        imageView?.alpha = 1.0
    }
    
    /// 调整内容边距
    public func setContentPadding(horizontal: CGFloat, vertical: CGFloat) {
        contentEdgeInsets = UIEdgeInsets(
            top: vertical,
            left: horizontal,
            bottom: vertical,
            right: horizontal
        )
    }
    
    /// 自动调整大小并居中内容
    public func sizeToFitCentered() {
        sizeToFit()
        centerContent()
    }
    
    /// 居中内容
    public func centerContent() {
        contentHorizontalAlignment = .center
        contentVerticalAlignment = .center
    }
}


